package edu.hawaii.ics414;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import edu.hawaii.ics414.GetGameList;

/**
 * The main screen when the app is opened. It sets all screens as they are swiped left and right.
 */
public class DummySectionFragment extends Fragment {
  public DummySectionFragment() {
  }

  public static final String ARG_SECTION_NUMBER = "section_number";

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final ListView listView = new ListView(getActivity());
    String[] games;
    ArrayList<String> fromServer = new ArrayList<String>();

    ArrayList<String> comingSoon = new ArrayList<String>();
    comingSoon.add("Assassin's Creed III");
    comingSoon.add("Little Big Planet Carting");
    comingSoon.add("Mugen Souls");
    comingSoon.add("Medal of Honor: Warfighter");
    comingSoon.add("Monster Hunter 3 ");

    Bundle args = getArguments();

    switch (args.getInt(ARG_SECTION_NUMBER)) {
    case 1:
      try {
        games =
            new GetGameList().execute(getString(R.string.title_section1)).get(5, TimeUnit.SECONDS);
        for (int i = 1; i < games.length; i++) {
          fromServer.add(games[i]);
        }
        ArrayAdapter<String> topRatedAdapter =
            new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1,
                fromServer);
        listView.setAdapter(topRatedAdapter);
        setListViewOnClickListner(listView);
      }
      catch (Exception e) {
        Toast.makeText(container.getContext(), "fail", Toast.LENGTH_LONG).show();
      }
      break;
    case 2:
      try {
        games =
            new GetGameList().execute(getString(R.string.title_section2)).get(5, TimeUnit.SECONDS);
        for (int i = 1; i < games.length; i++) {
          fromServer.add(games[i]);
        }
        ArrayAdapter<String> mostFavoritedAdapter =
            new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1,
                fromServer);
        listView.setAdapter(mostFavoritedAdapter);
        setListViewOnClickListner(listView);
      }
      catch (Exception e) {
        Toast.makeText(container.getContext(), "fail", Toast.LENGTH_LONG).show();
      }

      break;
    case 3:
      try {
        games =
            new GetGameList().execute(getString(R.string.title_section3)).get(5, TimeUnit.SECONDS);
        for (int i = 1; i < games.length; i++) {
          fromServer.add(games[i]);
        }
        ArrayAdapter<String> mostPopularAdapter =
            new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1,
                fromServer);
        listView.setAdapter(mostPopularAdapter);
        setListViewOnClickListner(listView);
        break;
      }
      catch (Exception e) {
        Toast.makeText(container.getContext(), "fail", Toast.LENGTH_LONG).show();
      }
    case 4:
      ArrayAdapter<String> comingSoonAdapter =
          new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1,
              comingSoon);
      listView.setAdapter(comingSoonAdapter);
      setListViewOnClickListner(listView);
      break;
    }
    return listView;
  }

  /*
   * Sets the on-click listener for the listviews when they are created.
   * 
   * @param listview The listview you want to set
   */
  public void setListViewOnClickListner(final ListView listView) {
    listView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String item = ((TextView) view).getText().toString();
        Intent myIntent = new Intent(view.getContext(), GameView.class);
        myIntent.putExtra("gameName", item);
        startActivityForResult(myIntent, 0);
      }
    });
  }
}